# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: conftest.py
# Time: 18:03

import pytest
import sys
import os

sys.path.insert(1, os.path.dirname(os.path.dirname(os.path.abspath(""))))
from common.utils.log_util import GenerateLog

log = None


def pytest_addoption(parser):
    parser.addoption("--cmdopt", action="store", default=None, help="将命令行参数，log_file日志文件添加到pytest配置中")


@pytest.fixture(scope="session")
def cmdopt(pytestconfig):
    # 两种写法
    return pytestconfig.getoption("--cmdopt")


@pytest.fixture(scope="session")
def log(cmdopt):
    global log
    log = GenerateLog(cmdopt).logger()
    return log


def pytest_collection_modifyitems(items):
    """ 测试用例收集完成时，将收集到的item的name和nodeid的中文显示在控制台上
        修改编码格式，解决用例名称包含中文乱码的问题
    """
    for item in items:
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")
